var Level = cc.Class({
    'extends': cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        levelArea: {
            'default': null,
            type: cc.Node
        },
        levelPrefab: {
            'default': null,
            type: cc.Prefab
        }
    },

    // use this for initialization
    onLoad: function onLoad() {
        //
        Level.instance = this;
        this.currentLevel = 1;
        // UI
        this.greyBgTexture = cc.textureCache.addImage(cc.url.raw("resources/My/select_bg_blk.png"));
        // main

        // 关卡状态，1解锁 2锁，将来从存储读取
        this.lockStatus = new Array(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        // 初始化选关页面
        this.showLevelPage(1);
    },

    showLevelPage: function showLevelPage() {

        for (var i = 0; i < 5; i++) {
            for (var j = 0; j < 5; j++) {
                var levelPrefab = cc.instantiate(this.levelPrefab);
                this.levelArea.addChild(levelPrefab);
                levelPrefab.setPosition(0 - (2 - j) * this.levelArea.width / 6, (2 - i) * this.levelArea.height / 8);
                var singleLevel = levelPrefab.getComponent('SingleLevel');
                singleLevel.levelNum = i * 5 + j + 1;
                singleLevel.levelLabel.getComponent(cc.Label).string = singleLevel.levelNum;
                if (this.lockStatus[singleLevel.levelNum - 1] === 0) {
                    // 未解锁，换背景，加锁
                    levelPrefab.getChildByName('selectBg').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.greyBgTexture);
                    levelPrefab.getChildByName('lock').active = true;
                } else {
                    // 解锁
                    levelPrefab.getChildByName('lock').active = false;
                }
            }
        }
    }

});

// called every frame, uncomment this function to activate update callback
// update: function (dt) {

// },
module.exports = Level;